﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using localText = Proxy.Properties.Resources;

namespace Proxy
{
    public class Auto_InfeedScanner
    {
        private Socket socket;
        private InsertInfoHandle Handle_Insert;
        private SearchInfoHandle Handle_Search;
        private HomagChina.Basic.CCT.Tools.SocketHelper socketHelper;

        public Auto_InfeedScanner(string Ip, int Port)
        {
            Handle_Insert = new InsertInfoHandle();
            Handle_Search = new SearchInfoHandle();
            //连接Scanner
            socketHelper = new HomagChina.Basic.CCT.Tools.SocketHelper();
            socket = socketHelper.Connect(Ip, Port);
            Task.Factory.StartNew(() => { MonitorDataReceive(socket, Ip, Port); });
        }

        public void MonitorDataReceive(Socket socket, string Ip, int Port)
        {
            byte[] bytes = new byte[1024];
        Start:
            while (true)
            {
                try
                {
                    Thread.Sleep(1000);
                    if (socket == null || !socket.Connected)
                    {
                        socket = socketHelper.Connect(Ip, Port);
                    }
                    if (!Globle.isInfeedAreaActive)
                    {
                        goto Start;
                    }
                    int bytesRec = socket.Receive(bytes);
                    if (bytesRec > 0)
                    {
                        if (Globle.isRunEmptyActive)
                        {
                            HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Run_Empty_function_is_running__loading_function_is_disabled);
                            goto Start;
                        }
                        //收到的消息
                        string Msg = Encoding.UTF8.GetString(bytes, 0, bytesRec).ToString().Replace('\u0002', ' ').Replace('\\', ' ').Trim();

                        SerilogHandle.Logger_AutoScanner.Information(Msg);

                        if (string.IsNullOrWhiteSpace(Msg) || Msg.Contains("Welcome") || Msg.Contains("ERROR"))
                        {
                            goto Start;
                        }

                        T_BoardInfo t_BoardInfo = Handle_Search.GetPartInfoByReference(Msg);
                        if (t_BoardInfo == null)
                        {
                            HomagGroup.Base.UI.Windows.MessageBox.Show(localText.The_board_data_does_not_exist_);
                            goto Start;
                        }

                        T_TransOnlineRecords t_TransOnlineRecords = new T_TransOnlineRecords()
                        {
                            Reference = Msg == "ERROR" ? Msg + "-" + DateTime.Now.ToString("HHmmssfff") : Msg,
                            Location = "RT010",
                            NextPosition = "BF015",
                            Direction = 10,
                            Area = "Infeed",
                            RouteId = 1,
                            State = 0,
                            Timestamp = DateTime.Now
                        };

                        var InfeedInfo = Handle_Search.GetTransOnLineDetailInfo("", "CT015");
                        if (Globle.OpreationMode.Equals("Auto") && InfeedInfo != null)
                        {
                            HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Board_information_exists);
                            goto Start;
                        }
                        else
                        {
                            Handle_Insert.InsertOnlinePart(t_TransOnlineRecords);
                            Handle_Insert.InsertScanRecords(Msg);
                        }
                    }
                    goto Start;
                }
                catch (Exception ex)
                {
                    socket.Dispose();
                    SerilogHandle.Logger_AutoScanner.Error(ex.Message);
                    goto Start;
                }
            }
        }
    }
}